package com.itic.mall.system.marketprofit.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.sys.common.service.OutCommonService;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.mall.system.marketprofit.service.MarketProfitService;
import com.itic.mall.system.marketuser.service.MarketUserService;
import com.itic.mall.system.user.service.MallUserService;
import com.itic.mall.system.marketprofit.persistence.model.MarketProfit;
import com.itic.mall.system.marketprofit.persistence.model.MarketProfitExample;


@Controller
@RequestMapping(value="/system/marketprofit")
public class MarketProfitController
     extends BaseController {
	@Autowired
    private MarketProfitService marketProfitService;
    
    @Autowired
    private MallUserService mallUserService;
    
    @Autowired
    private MarketUserService marketUserService;

    @Autowired
	protected OutCommonService outCommonService;
    
    //////////////////////////////////////////////////////常量区////////////////////////////////////////////////////////////////
    private static final String MODEL_NAME_CHINA = "返利记录";//中文名
    private static final String MODEL_NAME＿ENGLISH = "marketprofit";//英文名
    private static final String PAGE_COMMON_PATH = "mall/system/marketprofit/marketprofit";//通用路径
    private static final String COMMON_URL = "/system/marketprofit/";//通用路径

    //////////////////////////////////////////////////////页面跳转区////////////////////////////////////////////////////////////
   
    /*
     * 主页面
     */
    @RequestMapping(value="/init")
    public String init(Model model){
    	model.addAttribute("name_china", MODEL_NAME_CHINA);
    	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
    	model.addAttribute("common_url", COMMON_URL);
    	return PAGE_COMMON_PATH+"Main";
    }
    
    
    /**
     * <p>Title:queryCondition </p>
     * <p>Description:封装查询条件 </p>
     * @param jzdw
     * @param jzdwExample
     * @return
     */
    private MarketProfitExample queryCondition(HttpServletRequest request,MarketProfit temp) {
    	
    	// 参数处理
        MarketProfitExample example = new MarketProfitExample();
        // 按创建时间倒序
        example.setOrderByClause("CREATE_TIME DESC");
        
        example.createCriteria().andIsEffectiveEqualTo( DBConst.TRUE );
      
        
        // 查询条件
        if ( null != temp ) {
    	  
        }
        return example;
    }
    
    /**
     * 
     * @param goldType
     * @param request
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(MarketProfit Marketprofit, HttpServletRequest request){
        // 声明返回对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        
        MarketProfitExample example = this.queryCondition(request,Marketprofit);
        // 封装分页参数
        example = this.handlePageQuery(request, example);
        
        
        // 获取信息总条数
        int total = marketProfitService.count(example);
        
        // 获取数据
        List<MarketProfit> list = marketProfitService.list(example);
        if( list.size() > 0 ){
        	for( MarketProfit tmp : list ){
        		tmp.setUserId(mallUserService.get(tmp.getUserId()).getUserNickName());
        		tmp.setMarketUserId(marketUserService.get(tmp.getMarketUserId()).getUserNickName());
        	}
        }
        
        // 数据返回处理
        resultMap.put(IticConst.TOTAL_RECORDS, total);
        resultMap.put(IticConst.DISPLAY_RECORDS, total);
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        resultMap.put(IticConst.DATA, list);
        return resultMap;

    }

}